import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import React, { forwardRef, useContext } from "react";
import ThemeContext from "../../utils/ThemeContext";

var withFormOperate = function withFormOperate(WrappedComponent) {
  var Component = function Component(props, ref) {
    var _props$onChange = props.onChange,
        onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
        value = props.value;
    var theme = useContext(ThemeContext);

    var handleChange = function handleChange(name) {
      return function (v) {
        var _state;

        if (typeof value === "object" && value !== null) {
          _state = _objectSpread({}, props.value, _defineProperty({}, name, v));
        } else {
          _state = v;
        }

        onChange(_state);
      };
    };

    return React.createElement(WrappedComponent, Object.assign({}, props, {
      ref: ref,
      value: props.value,
      schema: theme.config.current.schema,
      styleCounts: theme.styleCounts,
      handleChange: handleChange
    }));
  };

  return forwardRef(Component);
};

export default withFormOperate;